﻿using System;
using System.Web;
using System.Web.Security;
using Chowpoo.Util;
using CourseSystem.Common.Models;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;
using CourseSystem.Core.Enum;

namespace CourseSystem.Common.Helpers
{
    public class UserHelper
    {
        private const string SessionName = "course#$%^User";

        public static UserModel LoginUser
        {
            get
            {
                var user = HttpContext.Current.Session[SessionName] as UserModel;
                if (user != null)
                {
                    return user;
                }
                var userIden = HttpContext.Current.User.Identity.Name;
                //TODO:测试环境可cookie登录,正式上线后将注释掉此段代码!
                var isTest = false;
#if DEBUG
                isTest = true;
#endif
                if (isTest && !string.IsNullOrEmpty(userIden))
                {
                    var userIdenArry = userIden.Split('_');
                    if (userIdenArry.Length == 2)
                    {
                        var type = (UserTypeEnum)userIdenArry[1].CheckByte(255);
                        var id = userIdenArry[0].CheckInt(0);
                        if (id == 0)
                        {
                            //HttpContext.Current.Response.Redirect(@"~/Account/LogOn");
                            return null;
                        }
                        switch (type)
                        {

                            case UserTypeEnum.Student:
                                var stu = StudentBLL.Get(id);
                                if (stu != null)
                                {
                                    user = new UserModel { Id = stu.Id, UserType = UserTypeEnum.Student, UserName = stu.StuName };
                                    SetLogined(user);
                                    return user;
                                } break;
                            case UserTypeEnum.Teacher:
                            case UserTypeEnum.Admin:
                                var tea = TeacherBLL.Get(id);
                                if (tea != null)
                                {
                                    user = new UserModel { IsExpert = tea.Type == 4, Id = tea.Id, MinType = tea.Type, LoginTime = DateTime.Now, UserType = tea.Type == 0 ? UserTypeEnum.Admin : UserTypeEnum.Teacher, UserName = tea.Name };
                                    SetLogined(user);
                                    return user;
                                } break;
                            default:
                                //HttpContext.Current.Response.Redirect(@"~/Account/LogOn");
                                return null;
                        }
                    }
                }
                //HttpContext.Current.Response.Redirect(@"~/Account/LogOn");
                return null;
            }
        }

        public static StudentInfo Student
        {
            get
            {
                if (LoginUser == null || LoginUser.UserType != UserTypeEnum.Student)
                    return null;
                var user = HttpContext.Current.Session["studentCourse"] as StudentInfo;
                if (user == null)
                {
                    var info = StudentBLL.Get(LoginUser.Id);
                    if (info == null)
                        return null;
                    HttpContext.Current.Session["studentCourse"] = info;
                    user = info;
                }
                return user;
            }
        }

        public static void UpdateStudent()
        {
            if (LoginUser == null || LoginUser.UserType != UserTypeEnum.Student)
                return;
            var info = StudentBLL.Get(LoginUser.Id);
            HttpContext.Current.Session["studentCourse"] = info;
        }

        public static int GetLoginUserType()
        {
            if (LoginUser.UserType == UserTypeEnum.Student)
            {
                return 1;
            }
            if (LoginUser.UserType == UserTypeEnum.Teacher || LoginUser.UserType == UserTypeEnum.Admin)
                return 2;
            return 1;
        }

        public static string GetUserName(int type, int id)
        {
            if (type == 1)
            {
                var stuInfo = StudentBLL.Get(id);
                if (stuInfo != null)
                    return stuInfo.StuName;
            }
            else if (type == 2 || type == 3)
            {
                var userInfo = TeacherBLL.Get(id);
                if (userInfo != null)
                    return userInfo.Name;
            }
            return "";
        }

        public static void SetLogined(UserModel info)
        {
            HttpContext.Current.Session[SessionName] = info;
            FormsAuthentication.SetAuthCookie(info.Id.ToString() + "_" + (byte)info.UserType, true);
        }

        public static void LogOut()
        {
            HttpContext.Current.Session.RemoveAll();
            FormsAuthentication.SignOut();
        }

        public static string NotAuthUrl()
        {
            var re = Chowpoo.Common.CommonRequest.GetUrlReferrer();
            if (string.IsNullOrEmpty(re) || re == HttpContext.Current.Request.Url.AbsoluteUri)
                re = CommonHelper.Domain + "Account/Logon";
            return re;
        }
    }
}